package com.leimo.service.impl;

import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.leimo.service.WechatPayService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

/**
 * 微信支付服务实现类
 *
 * @author 段誉
 * @create 2019-04-19 15:19
 */
@Slf4j
@Service
public class WechatPayServiceImpl implements WechatPayService {
  @Autowired
  private WxPayService wxPayService;

  /**
   * 商铺名称
   */
  private static final String SEND_NAME = "测试";

  //微信支付返回状态信息
  /**
   * 成功
   */
  private static final String SUCCESS = "SUCCESS";
  /**
   * 失败
   */
  private static final String FAIL = "FAIL";
  //红包发送状态
  /**
   * 发放中
   */
  private static final String SENDING = "SENDING";
  /**
   * 已发放待领取
   */
  private static final String SENT = "SENT";
  /**
   * 发放失败
   */
  private static final String FAILED = "FAILED";
  /**
   * 已领取
   */
  private static final String RECEIVED = "RECEIVED";
  /**
   * 退款中
   */
  private static final String RFUND_ING = "RFUND_ING";
  /**
   * 已退款
   */
  private static final String REFUND = "REFUND";

  /**
   * 发送红包
   * @param openId
   * @return com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult
   * @author 段誉
   * @date 2019/4/19 15:20
   * @since 1.0.0
   */
  @Override
  public void sendRedpack(String openId) throws WxPayException {
    WxPaySendRedpackRequest payRequest = new WxPaySendRedpackRequest();
    //商铺名称
    payRequest.setSendName(SEND_NAME);
    //用户openId
    payRequest.setReOpenid(openId);
    //红包总额
    payRequest.setTotalAmount(1);
    //红包发放总人数
    payRequest.setTotalNum(1);
    //设置祝福语
    payRequest.setWishing("测试");
    //设置调用接口的机器Ip地址,(需要找缪海燕添加ip白名单)
    payRequest.setClientIp("");
    //设置活动名称
    payRequest.setActName("测试");
    //设置备注
    payRequest.setRemark("测试");
    //设置场景(企业内部福利)
    payRequest.setSceneId("PRODUCT_4");

    //发送红包后的返回状态信息
    WxPaySendRedpackResult sendRedpackResult = wxPayService.sendRedpack(payRequest);
    //如果状态为fail则直接返回发送失败
    if (sendRedpackResult.getReturnCode().equals(FAIL)) {
      throw new RuntimeException(sendRedpackResult.getReturnMsg());
    }

    //发送成功
    if (sendRedpackResult.getResultCode().equals(SUCCESS)) {
      return;
    }
    log.error("-------------------微信发送红包失败-------------------");

    log.error("发送红包的错误代码:[{}]", sendRedpackResult.getErrCode());
    log.error("发送红包的错误代码描述:[{}]", sendRedpackResult.getErrCodeDes());

    //resultCode为fail则证明发送状态不明确需要重新查询发送结果
    WxPayRedpackQueryResult redpackQueryResult =
            wxPayService.queryRedpack(sendRedpackResult.getMchBillno());

    //查询失败直接返回失败
    if (redpackQueryResult.getReturnCode().equals(FAIL)) {
      throw new RuntimeException(redpackQueryResult.getReturnMsg());
    }

    //红包信息的业务状态为fail则直接返回
    if (redpackQueryResult.getResultCode().equals(FAIL)) {
      log.error("查询微信红包信息失败，错误代码:[{}]", redpackQueryResult.getErrCode());
      throw new RuntimeException(redpackQueryResult.getErrCodeDes());
    }

    //判断如果红包发送失败则提示失败原因
    if (redpackQueryResult.getStatus().equals(FAILED)) {
      throw new RuntimeException(redpackQueryResult.getReason());
    }
  }

  /**
   * 查询红包信息
   *
   * @param mchBillno 商户订单号
   * @return java.lang.Object
   * @author 段誉
   * @date 2019/4/19 15:56
   * @since 1.0.0
   */
  @Override
  public WxPayRedpackQueryResult searchRedpackInfo(String mchBillno) throws WxPayException {
    return wxPayService.queryRedpack(mchBillno);
  }
}
